//
//  WPLoginViewController.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/3/30.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPMsgLoginViewController.h"

@interface WPMsgLoginViewController ()
@property (nonatomic, strong) UIImageView * backImageView;/**< 背景图 */
@property (nonatomic, strong) UIImageView * logoImageView;/**< 图标 */
@property (nonatomic, strong) UITextField * accountTextField;/**< 账号输入框 */
@property (nonatomic, strong) UITextField * pwdTextField;/**< 密码输入框 */
@property (nonatomic, strong) UITextField * codeTextField;/**< 验证码输入框 */
@property (nonatomic, strong) UIButton * getCodeBtn;/**< 获取验证码按钮 */
@property (nonatomic, strong) UIButton * protocolBtn;/**< 同意协议按钮 */
@property (nonatomic, strong) UILabel * protocolLabel;/**< 用户协议 */
@property (nonatomic, strong) UIButton * loginBtn;/**< 登录按钮 */
@property (nonatomic, strong) UIButton * pwdLoginBtn;/**< 密码登录按钮 */
@property (nonatomic, strong) UIButton * registerBtn;/**< 注册按钮 */
@property (nonatomic, strong) NSTimer * timer;/**< 计时器 */
@property (nonatomic, assign) NSInteger timeCount;/**< 时间记录 */
- (void)initializeUserInterface; /**< 初始化用户界面 */
@end

@implementation WPMsgLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeUserInterface];
}
#pragma mark - init
- (void)initializeUserInterface {
    self.navigationController.navigationBarHidden = YES;
    [self.view addSubview:self.backImageView];
    [self.view addSubview:self.logoImageView];
    [self.view addSubview:self.accountTextField];
    UIBezierPath * path = [UIBezierPath bezierPath];
    [self.view addSubview:self.accountTextField];
    [self addLineWithStart:CGPointMake(CGRectGetMinX(_accountTextField.frame), CGRectGetMaxY(_accountTextField.frame)) andEndPoint:CGPointMake(CGRectGetMaxX(_accountTextField.frame), CGRectGetMaxY(_accountTextField.frame)) andWithPath:path];
    [self.view addSubview:self.pwdTextField];
    [self.view addSubview:self.getCodeBtn];
    [self addLineWithStart:CGPointMake(CGRectGetMinX(_pwdTextField.frame), CGRectGetMaxY(_pwdTextField.frame)) andEndPoint:CGPointMake(CGRectGetMaxX(_getCodeBtn.frame), CGRectGetMaxY(_pwdTextField.frame)) andWithPath:path];
    [self.view addSubview:self.protocolBtn];
    [self.view addSubview:self.protocolLabel];
    [self.view addSubview:self.loginBtn];
    [self.view addSubview:self.pwdLoginBtn];
    [self.view addSubview:self.registerBtn];
    CAShapeLayer * layer = [CAShapeLayer layer];
    layer.frame = DHFlexibleFrame(CGRectMake(0, 0, 320, 800), NO);
    layer.fillColor = [UIColor whiteColor].CGColor;
    layer.backgroundColor = [UIColor clearColor].CGColor;
    layer.strokeColor = COLOR_RGB(210, 211, 212, 1).CGColor;/**< 画笔颜色 */
    layer.path = path.CGPath;
    layer.lineWidth = 0.8;
    [self.view.layer addSublayer:layer];
}
#pragma mark - responds events
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
- (void)respondToGetCodeBtn:(UIButton *)sender {
    [self.view endEditing:YES];
    if (_accountTextField.text.length == 0 || ![ConfirmMobileNumber isMobileNumber:_accountTextField.text]) {
        [self initializeAlertControllerWithMessage:@"请输入正确格式的手机号码"];
        return;
    }
    [sender setTitle:@"60s再次获取" forState:UIControlStateNormal];
    sender.userInteractionEnabled = NO;
    [self.timer setFireDate:[NSDate distantPast]];
    _timeCount = 60;
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.color = [UIColor grayColor];
    hud.labelText = @"请稍等";
    hud.detailsLabelText = @"正在发送验证码...";
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [hud hide:YES];
    });
}
- (void)respondsToTimer {
    self.timeCount--;
    NSString * timeStr = [NSString stringWithFormat:@"%lds再次获取", (long)_timeCount];
    [_getCodeBtn setTitle:timeStr forState:UIControlStateNormal];
    if (_timeCount == 0) {
        [self.timer setFireDate:[NSDate distantFuture]];
        [_getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        _getCodeBtn.userInteractionEnabled = YES;
    }
}
- (void)respondsToProtocolBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
}
- (void)respondsToUserProtocol {
    [self pushToNextController:[[NSClassFromString(@"WPUserProtocolViewController") alloc]init]withVCTitle:@"注册登录协议"];
}
- (void)respondToLoginBtn {
    [self.view endEditing:YES];
}
- (void)respondToPwdLoginBtn {
    [self.view endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)respondToRegisterBtn {
    [self.view endEditing:YES];
    [self pushToNextController:[[NSClassFromString(@"WPRegisterViewController") alloc]init] withVCTitle:nil];
    
}
#pragma mark - private methods
- (void)addLineWithStart :(CGPoint)start andEndPoint:(CGPoint)end andWithPath:(UIBezierPath *)path{
    [path moveToPoint:start];
    [path addLineToPoint:end];
}
#pragma mark - getter
- (UIImageView *)backImageView {
    if (!_backImageView) {
        _backImageView = ({
            UIImageView * view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height)];
            view.image = IMAGE_NAME(@"图层-2.png");
            view;
        });
    }
    return _backImageView;
}
- (UIImageView *)logoImageView {
    if (!_logoImageView) {
        _logoImageView = ({
            UIImageView * view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30*DHFlexibleHorizontalMutiplier(), 50*DHFlexibleHorizontalMutiplier())];
            view.center = CGPointMake(self.view.center.x, 108*DHFlexibleVerticalMutiplier());
            view.image = IMAGE_NAME(@"LOGO-拷贝.png");
            view;
        });
    }
    return _logoImageView;
}
- (UITextField *)accountTextField {
    if (!_accountTextField) {
        _accountTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(35*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.logoImageView.frame)+110*DHFlexibleVerticalMutiplier(), 250*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            text.leftViewMode = UITextFieldViewModeAlways;
            UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            UIImageView * userImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 7*DHFlexibleHorizontalMutiplier(), 15*DHFlexibleHorizontalMutiplier(), 15*DHFlexibleHorizontalMutiplier())];
            userImage.image = IMAGE_NAME(@"iconfont-yonghuming.png");
            [leftView addSubview:userImage];
            text.leftView = leftView;
            UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(text.bounds), text.bounds.size.width, 1)];
            line.backgroundColor = [UIColor whiteColor];
            [text addSubview:line];
            text.keyboardType = UIKeyboardTypePhonePad;
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            text.textColor = [UIColor whiteColor];
            text.clearButtonMode = UITextFieldViewModeWhileEditing;
            NSMutableDictionary *dict = [NSMutableDictionary dictionary]; dict[NSForegroundColorAttributeName] = [UIColor whiteColor]; NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"请输入注册手机号" attributes:dict];
            text.attributedPlaceholder = str;
            text;
        });
    }
    return _accountTextField;
}
- (UITextField *)pwdTextField {
    if (!_pwdTextField) {
        _pwdTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(35*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.accountTextField.frame)+15*DHFlexibleVerticalMutiplier(), 180*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            text.leftViewMode = UITextFieldViewModeAlways;
            UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            UIImageView * userImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 7*DHFlexibleHorizontalMutiplier(), 15*DHFlexibleHorizontalMutiplier(), 15*DHFlexibleHorizontalMutiplier())];
            userImage.image = IMAGE_NAME(@"iconfont-mima.png");
            [leftView addSubview:userImage];
            text.leftView = leftView;
            text.keyboardType = UIKeyboardTypeDefault;
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            text.textColor = [UIColor whiteColor];
            text.clearButtonMode = UITextFieldViewModeWhileEditing;
            NSMutableDictionary *dict = [NSMutableDictionary dictionary]; dict[NSForegroundColorAttributeName] = [UIColor whiteColor]; NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:dict];
            text.attributedPlaceholder = str;
            text;
        });
    }
    return _pwdTextField;
}
- (UIButton *)getCodeBtn {
    if (!_getCodeBtn) {
        _getCodeBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(225*DHFlexibleHorizontalMutiplier(), CGRectGetMinY(self.pwdTextField.frame)+5*DHFlexibleHorizontalMutiplier(), 60*DHFlexibleHorizontalMutiplier(), 25*DHFlexibleHorizontalMutiplier());
            btn.layer.cornerRadius = 5*DHFlexibleHorizontalMutiplier();
            btn.backgroundColor = COLOR_RGB(221, 221, 221, 1);
            btn.titleLabel.font = [UIFont systemFontOfSize:10*DHFlexibleHorizontalMutiplier()];
            [btn setTitleColor:COLOR_RGB(116, 116, 116, 1) forState:UIControlStateNormal];
            [btn setTitle:@"获取验证码" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondToGetCodeBtn:) forControlEvents:UIControlEventTouchUpInside];
            
            btn;
        });
    }
    return _getCodeBtn;
}
- (UIButton *)protocolBtn {
    if (!_protocolBtn) {
        _protocolBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(35 *DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.pwdTextField.frame)+17.5*DHFlexibleVerticalMutiplier(), 10*DHFlexibleHorizontalMutiplier(), 10*DHFlexibleHorizontalMutiplier());
            [btn setImage:IMAGE_NAME(@"tongyixieyik.png") forState:UIControlStateNormal];
            [btn setImage:IMAGE_NAME(@"iconfont-fuxuankuang.png") forState:UIControlStateSelected];
            [btn addTarget:self action:@selector(respondsToProtocolBtn:) forControlEvents:UIControlEventTouchUpInside];
            btn;
            
        });
    }
    return _protocolBtn;
}
- (UILabel *)protocolLabel {
    if (!_protocolLabel) {
        _protocolLabel = ({
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(55*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.pwdTextField.frame)+5*DHFlexibleVerticalMutiplier(), 100*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            label.font = [UIFont systemFontOfSize:10*DHFlexibleHorizontalMutiplier()];
            label.textColor = [UIColor whiteColor];
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(respondsToUserProtocol)];
            [label addGestureRecognizer:tap];
            label.userInteractionEnabled = YES;
            label.text = @"同意协议";
            label;
        });
    }
    return _protocolLabel;
}
- (UIButton *)loginBtn {
    if (!_loginBtn) {
        _loginBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(35*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.protocolLabel.frame)+30*DHFlexibleHorizontalMutiplier(), 250*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            btn.layer.cornerRadius = 15*DHFlexibleHorizontalMutiplier();
            [btn setTitle:@"登  录" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondToLoginBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _loginBtn;
}
- (UIButton *)pwdLoginBtn {
    if (!_pwdLoginBtn) {
        _pwdLoginBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(35*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.loginBtn.frame)+20*DHFlexibleHorizontalMutiplier(), 60*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.titleLabel.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            [btn setTitle:@"密码登陆" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondToPwdLoginBtn) forControlEvents:UIControlEventTouchUpInside];
            
            btn;
        });
    }
    return _pwdLoginBtn;
}
- (UIButton *)registerBtn {
    if (!_registerBtn) {
        _registerBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(235*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.loginBtn.frame)+20*DHFlexibleHorizontalMutiplier(), 60*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.titleLabel.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            [btn setTitle:@"立即注册" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondToRegisterBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _registerBtn;
}
- (NSTimer *)timer {
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(respondsToTimer) userInfo:nil repeats:YES];
    }
    return _timer;
}
@end
